package com.wuhao.shootclient.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wuhao.shootclient.R;

import java.util.List;

/**
 * 靶子--
 * Create by zhaogong on 2019-10-30
 */
public class TargetView extends FrameLayout {

    private Context context;
    private ViewGroup rootView;

    //绿色射击点  宽度  （宽高一致）
    private int greenTargetWidth;
    //红色射击点  宽度  （宽高一致）
    private int redTargetWidth;

    //红色射击点  闪烁速度
    private int redTargetFlashSpeed;
    Animation animation;

    private double mWidth = 0.0;
    private double mHeight = 0.0;

    public TargetView(@NonNull Context context) {
        this(context, null);
    }

    public TargetView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TargetView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        //FrameLayout自身不调用onDraw
        setWillNotDraw(false);
        rootView = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.view_target, this, true);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TargetView);
        greenTargetWidth = (int) typedArray.getDimension(R.styleable.TargetView_greenTargetWidth, 24);
        redTargetWidth = (int) typedArray.getDimension(R.styleable.TargetView_redTargetWidth, 24);
        redTargetFlashSpeed = typedArray.getInt(R.styleable.TargetView_redTargetFlashSpeed, 800);

        animation = new AlphaAnimation(1f, 0.2f); // Change alpha from fully visible to invisible
        animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
        animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
        animation.setRepeatMode(Animation.REVERSE); //
        typedArray.recycle();

    }

    /**
     * 设置 绿色射击点  宽度
     *
     * @param width
     */
    public void setGreenTargetWidth(int width) {
        greenTargetWidth = width;
    }

    /**
     * 设置红色射击点  宽度
     *
     * @param width
     */
    public void setRedTargetWidth(int width) {
        redTargetWidth = width;
    }

    /**
     * 设置 红色射击点闪烁间隔时间
     *
     * @param time
     */
    public void setRedTargetFlashSpeed(int time) {
        redTargetFlashSpeed = time;
    }


    /**
     * 设置所有射击点，必须加同步锁
     */
    public void showAllTargets(List<Point> points,int width,int height,int pattern) {
        if (points == null) {
            return;
        }
        synchronized (TargetView.class) {
            if (redTargetView != null) {
                redTargetView.clearAnimation();
            }
            rootView.removeAllViews();
            for (int i = 0; i < points.size(); i++) {
                Point point = points.get(i);
                //脱吧不点点
                if (point.x != width && point.y != height) {
                    //最后一枪红色
                    if (i == (points.size() - 1)) {
//                        setRedTarget((int) (point.x), (int) (point.y));
                        setRedTarget((int) (point.x), (int) (point.y),pattern);
                    } else {
                        //其他绿色
//                        setGreenTarget((int) (point.x),  (int) (point.y));
                        setGreenTarget((int) (point.x), (int) (point.y),pattern);
                    }
                }
            }
        }


    }


    /**
     * 设置绿色射击点
     *
     * @param x
     * @param y
     */
    private void setGreenTarget(int x, int y,int pattern) {
        LayoutParams targetFLP = new LayoutParams(greenTargetWidth, greenTargetWidth);

        targetFLP.gravity = (Gravity.TOP | Gravity.LEFT);


        ImageView greeTargetView = new ImageView(context);
        //模拟靶
        if (pattern == 0){
            greeTargetView.setImageResource(R.mipmap.pos_target_green);
        }
        //实图靶
        else if (pattern == 1){
            greeTargetView.setImageResource(R.mipmap.real_graph_sign_green);
        }

        targetFLP.topMargin = y - targetFLP.height/2;
        targetFLP.leftMargin = x - targetFLP.width/2;

        greeTargetView.setLayoutParams(targetFLP);

        rootView.addView(greeTargetView);

    }

    ImageView redTargetView;

    /**
     * 设置红色射击点
     *
     * @param x
     * @param y
     */
    private void setRedTarget(int x, int y,int pattern) {
        LayoutParams targetFLP = new LayoutParams(redTargetWidth, redTargetWidth);

        targetFLP.gravity = (Gravity.TOP | Gravity.LEFT);

        redTargetView = new ImageView(context);

        //模拟靶
        if (pattern == 0){
            redTargetView.setImageResource(R.mipmap.pos_target_red);
        }
        //实图靶
       else if (pattern == 1){
            redTargetView.setImageResource(R.mipmap.real_graph_sign_red);
        }

        animation.setDuration(redTargetFlashSpeed);
        redTargetView.setAnimation(animation);

        targetFLP.topMargin = y - targetFLP.height/2;
        targetFLP.leftMargin = x - targetFLP.width/2;

        redTargetView.setLayoutParams(targetFLP);

        rootView.addView(redTargetView);

    }

    public void setTranslate(double Width,double Height){
        mWidth = Width;
        mHeight = Height;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //平移  改变坐标系原点位置
//        if (newCanvas != null){
            canvas.translate((float) mWidth,(float) mHeight);
//        LogUtil.error("mWidth"+mWidth+"mHeight"+mHeight);
//            newCanvas = null;
//        }
//        else {
//            canvas.translate((float) (this.getWidth()*0.5),(float) (this.getHeight()*0.5965483555845002));
//        LogUtil.error("mWidth"+mWidth+"mHeight"+mHeight);

//        }
    }
}